package com.ybzl.gateway.utils;

import cn.hutool.core.date.DateUtil;

import java.util.Collections;
import java.util.Date;

public class StringToHexUtil {

    public static byte[] toByte(String data) {
        assert data == null : "data not null";
        String[] splitData = data.split(" ");
        byte[] sendData = new byte[splitData.length];
        for (int i = 0; i < splitData.length; i++) {
            sendData[i] = (byte) Integer.parseInt(splitData[i], 16);
        }
        return sendData;
    }

    /**
     * 小端
     *
     * @param data
     * @return
     */
    public static String toStringSmall(byte[] data) {
        StringBuilder hexString = new StringBuilder();
        for (int i = data.length - 1; i >= 0; i--) {
            byte b = data[i];
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                hexString.append('0'); // 确保每个字节表示为两个十六进制数字
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }

    public static String toString(byte[] data) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : data) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                hexString.append('0'); // 确保每个字节表示为两个十六进制数字
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }

    public static String toString(byte data) {
        StringBuilder hexString = new StringBuilder();
        String hex = Integer.toHexString(0xff & data);
        if (hex.length() == 1) {
            hexString.append('0'); // 确保每个字节表示为两个十六进制数字
        }
        hexString.append(hex);
        return hexString.toString();
    }

    public static String toDec(byte[] data) {
        StringBuilder decString = new StringBuilder();
        for (byte b : data) {
            int dec = Integer.parseInt(String.format("%02x", b), 16);
            decString.append(dec);
        }
        return decString.toString();
    }

    public static String toDec(byte data) {
        StringBuilder decString = new StringBuilder();
        int dec = Integer.parseInt(String.format("%02x", data), 16);
        decString.append(dec);
        return decString.toString();
    }

    public static String toDateTime() {
        Date date = DateUtil.date(System.currentTimeMillis());
        long dateTime = date.getTime() / 1000;
        char[] chars = Long.toHexString(dateTime).toCharArray();
        StringBuilder builder = new StringBuilder();
        builder.append(chars[0]);
        builder.append(chars[1]);
        builder.append(" ");
        builder.append(chars[2]);
        builder.append(chars[3]);
        builder.append(" ");
        builder.append(chars[4]);
        builder.append(chars[5]);
        builder.append(" ");
        builder.append(chars[6]);
        builder.append(chars[7]);
        return builder.toString();
    }

    public static int twoBytesToIntLittleEndian(byte[] bytes) {
        if (bytes.length != 2) {
            throw new IllegalArgumentException("需要恰好2个字节");
        }
        return ((bytes[1] & 0xFF) << 8) | (bytes[0] & 0xFF);
    }
}
